package com.wva.cookie;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

/**
 *  Cookie的基本使用
 */
@WebServlet("/demo01Cookie")
public class Demo01Cookie extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.创建Cookie对象
        Cookie c1 = new Cookie("msg","hello");
        Cookie c2 = new Cookie("name","zhangsan");
        Cookie c3 = new Cookie("msg","你好");  //8.0之后可以存中文,之前不允许需要转URL编码(%E3)

        //2.设置cookie的时效
        //c1.setMaxAge(30);//将cookie持久化到硬盘，30秒后会自动删除cookie文件
        //c1.setMaxAge(-1); //默认
//        c1.setMaxAge(0);//删除Cookie

        //设置path，让当前服务器下部署的所有项目共享Cookie信息
//        c1.setPath("/");


        //3.发送多个Cookie
        response.addCookie(c1);
        response.addCookie(c2);
        response.addCookie(c3);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
        this.doPost(request, response);
    }

}